﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using System.Data.OleDb;
using System.Data;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;

namespace WPMuiTool
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = Environment.CurrentDirectory + @"\sample.txt";
            string sampleText = File.ReadAllText(path);
            string excelPath = Environment.CurrentDirectory + @"\Mui.xls";

            string connectFormat = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0";
            string connectionstr = string.Format(connectFormat, excelPath);
            DataTable dtMuiText;
            DataTable dtMuiLanguage;

            Console.WriteLine("Read excel begin--------------------");
            using (OleDbConnection connection = new OleDbConnection(connectionstr))
            {
                DataSet ds = new DataSet();
                connection.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter("select * from [muitext$]", connection);
                adapter.Fill(ds, "[muitext$]");
                dtMuiText = ds.Tables[0];
                adapter = new OleDbDataAdapter("select * from [language$]", connection);
                adapter.Fill(ds, "[language$]");
                dtMuiLanguage = ds.Tables[1];
            }

            Console.WriteLine("Read Language begin--------------------");
            Dictionary<string, string> dictLanguage = new Dictionary<string, string>();
            for (int i = 0; i < dtMuiLanguage.Rows.Count; i++)
            {
                string language = dtMuiLanguage.Rows[i][1].ToString();
                string dllName = dtMuiLanguage.Rows[i][2].ToString();
                if (!dictLanguage.Keys.Contains(language))
                {
                    Console.WriteLine("language:{0}-->{1}", language, dllName);
                    dictLanguage.Add(language, dllName);
                }
            }

            Console.WriteLine("Read mui OK====================");

            for (int j = 2; j < dtMuiText.Columns.Count; j++)
            {
                string outputText = sampleText;
                string languageName = dtMuiText.Columns[j].Caption;
                outputText = outputText.Replace("$language$", languageName);
                for (int i = 0; i < dtMuiText.Rows.Count; i++)
                {
                    string muiKey = dtMuiText.Rows[i][0].ToString();
                    string muiValue = dtMuiText.Rows[i][j].ToString();
                    outputText = outputText.Replace("$" + muiKey + "$", muiValue);
                }

                string fileName = string.Format("AppResources.{0}.resx", languageName);
                Console.WriteLine("write language file:{0}", fileName);
                File.WriteAllText(Environment.CurrentDirectory + @"\" + fileName, outputText, Encoding.Unicode);
            }
            Console.WriteLine("all are ok...................");


            string sampleRCPath = Environment.CurrentDirectory + @"\AppResLib.rc.sampe";
            string rcPath = Environment.CurrentDirectory + @"\AppResLib\AppResLib.rc";
            string projectFile = Environment.CurrentDirectory + @"\AppResLib\AppResLib.vcxproj";
            string outputPath = Environment.CurrentDirectory + @"\AppResLib\output";
            string outputDllPath = outputPath + @"\AppResLib.dll";
            string sampleRC = File.ReadAllText(sampleRCPath);

            for (int j = 2; j < dtMuiText.Columns.Count; j++)
            {
                string languageName = dtMuiText.Columns[j].Caption;

                for (int i = 0; i < dtMuiText.Rows.Count; i++)
                {
                    string muiKey = dtMuiText.Rows[i][0].ToString();
                    string muiValue = dtMuiText.Rows[i][j].ToString();
                    sampleRC = sampleRC.Replace("$" + muiKey + "$", muiValue);
                }
                File.WriteAllText(rcPath, sampleRC);

                var txt = File.ReadAllText(projectFile);
                txt = txt.Replace("$OutPutPath$", outputPath);
                File.WriteAllText(projectFile, txt);

                buildLanguage(projectFile);

                File.Move(outputDllPath, outputPath + @"\" + dictLanguage[languageName]);
            }
            Console.Read();
        }


        static void buildLanguage(string projectFile)
        {
            var props = new Dictionary<string, string>();
            // props["Configuration"] = "Debug";
            props["Configuration"] = "Release";
            //props["Platform"] = "Win32";
            //if c++
            //props["OutDir"] = @"S:\dddd\";
            //if c#
            props["OutputPath"] = @"S:\dddd\";
            var request = new BuildRequestData(projectFile, props, null, new string[] { "Build" }, null);
            var parms = new BuildParameters();
            parms.Loggers = new List<ILogger> { new ConsoleLogger() };

            var result = BuildManager.DefaultBuildManager.Build(parms, request);
            Console.WriteLine(result);
        }
    }
}
